<?php
// Initialisation
require('inc/init.inc.php');
require(PATH.'/class/gestion/clients.class.php');

class clientpdf extends Client{
	
function Pdf($nb_comm = -1, $nb_con = -1){
	$pdf=new MonFPDFHeader($this->attribut['cli_nom']);
	
	$pdf->AddPage();

	$hauteur = 8;
	
	// Utilisateur
	$uti = '';
	$req =	"SELECT uti_code, uti_nom, uti_prenom
			FROM utilisateurs
			WHERE uti_id='".$this->attribut['cli_uti_id']."'";
	if ($result = $this->db->Query($req)){
		if ($result->num_rows){
			$row = $result->fetch_assoc();
			$uti = $row['uti_code'].' - '.$row['uti_nom'].', '.$row['uti_prenom'];
		}
	}
	$pdf->SetFont('Arial','B',10);
	$y = $pdf->GetY();
	$pdf->MultiCell(95, $hauteur, utf8_decode($uti), 0);
	
	// Branche
	$bra = '';
	$req =	"SELECT bra_code, bra_nom
			FROM branches
			WHERE bra_id='".$this->attribut['cli_bra_id']."'";
	if ($result = $this->db->Query($req)){
		if ($result->num_rows){
			$row = $result->fetch_assoc();
			$bra = $row['bra_code'].' - '.$row['bra_nom'];
		}
	}
	$pdf->SetXY(105,$y);	
	$pdf->MultiCell(95, $hauteur, utf8_decode($bra), 0, 'R'); 

	// Ligne séparatrice
	$pdf->Separateur(0);
	
	// Zones
	$zon = '';
	$req =	"SELECT zon_code, zon_nom
			FROM zones
			WHERE zon_id='".$this->attribut['cli_zon_id']."'";
	if ($result = $this->db->Query($req)){
		if ($result->num_rows){
			$row = $result->fetch_assoc();
			$zon = $row['zon_code'].' - '.$row['zon_nom'];
		}
	}
	$pdf->SetX(105);	
	$pdf->MultiCell(95, $hauteur, utf8_decode($zon), 0, 'R'); 

	//Adresse
	$pdf->SetY($pdf->GetY()-$hauteur);
	$hauteur = 8;
	$pdf->SetFont('Arial','U',10);
	$pdf->MultiCell(0, $hauteur, 'Adresse:', 0);
	$hauteur = 7;
	$pdf->SetFont('Arial','',10);
	$pdf->MultiCell(0, $hauteur, utf8_decode($this->attribut['cli_adresse']), 0);
	
	// Boite postal
	$pdf->SetFont('Arial','U',10);
	$pdf->Write($hauteur, 'Boite postale:');
	$pdf->SetFont('Arial', '', 10);
	$pdf->Write($hauteur, ' '.utf8_decode($this->attribut['cli_boitepostale']));

	// Code postal
	$pdf->SetX(10+190/4);
	$pdf->SetFont('Arial','U',10);
	$pdf->Write($hauteur, 'Code postal:');
	$pdf->SetFont('Arial', '', 10);
	$pdf->Write($hauteur, ' '.utf8_decode($this->attribut['cli_codepostal']));

	// Ville
	$pdf->SetX(10+190/2);
	$pdf->SetFont('Arial','U',10);
	$pdf->Write($hauteur, 'Ville:');
	$pdf->SetFont('Arial', '', 10);
	$pdf->Write($hauteur, ' '.utf8_decode($this->attribut['cli_ville']));
	$pdf->AddY($hauteur);

	// Téléphone
	$pdf->SetStyle(PDF_LABEL);
	$pdf->Cell(190*0.2, $hauteur, utf8_decode('téléphone:'), 0, 0, 'R');
	$pdf->SetStyle(PDF_INFO);
	$pdf->Cell(190*0.3, $hauteur, utf8_decode($this->attribut['cli_telephone']), 0, 0, '');
	// Email
	$pdf->SetStyle(PDF_LABEL);
	$pdf->Cell(190*0.2, $hauteur, 'E-mail:', 0, 0, 'R');
	$pdf->SetStyle(PDF_INFO);
	$pdf->Cell(190*0.3, $hauteur, utf8_decode($this->attribut['cli_email']), 0, 0, '');
	$pdf->Ln();
	// Fax
	$pdf->SetStyle(PDF_LABEL);
	$pdf->Cell(190*0.2, $hauteur, 'Fax:', 0, 0, 'R');
	$pdf->SetStyle(PDF_INFO);
	$pdf->Cell(190*0.3, $hauteur, utf8_decode($this->attribut['cli_fax']), 0, 0, '');
	// TVA
	$pdf->SetStyle(PDF_LABEL);
	$pdf->Cell(190*0.2, $hauteur, 'TVA:', 0, 0, 'R');
	$pdf->SetStyle(PDF_INFO);
	$pdf->Cell(190*0.3, $hauteur, utf8_decode($this->attribut['cli_tva']), 0, 0, '');
	$pdf->AddY($hauteur);
	
	$hauteur = 5;
	
	// Contacts
	$pdf->Separateur($hauteur,'Contacts');
	$sql='SELECT con_nom, con_prenom, con_fonction, con_email, con_annee, con_portable, con_telephone'.
		' FROM contacts'.
		' JOIN clients ON clients.cli_id = contacts.con_cli_id'.
		" WHERE con_cli_id ='".$this->attribut['cli_id']."'";
	if ($nb_con>1)
		$sql .= ' LIMIT 0, '.$nb_con;
	if ($result = $this->db->Query($sql)){
		$th = array('nom', 'fonction', 'telephone', 'e-mail', 'MAJ');
		$data= array();
		$largeurs = array(190*0.25,190*0.25,190*0.15,190*0.25,190*0.1);
		while ($row = $result->fetch_assoc()) {
			if ( $row['con_portable'] )
				$tel = $row['con_portable'];
			elseif ( $row['con_telephone'] )
				$tel = $row['con_telephone'];
			else
				$tel = '';
			$data[] = array($row['con_nom'].', '.$row['con_prenom'], $row['con_fonction'], $tel, $row['con_email'], $row['con_annee']);
		}
		$pdf->Tableau($th, $data, $largeurs, $hauteur);
	}
	
	// Machines
	$pdf->Separateur($hauteur,'Machines', $hauteur);
	$sql='SELECT mod_id, mod_code, mac_serie, mac_commission, mac_annee, mac_id, cli_id, cli_nom'.
		' FROM machines'.
		' JOIN clients ON machines.mac_cli_id = clients.cli_id'.
		' JOIN modeles ON modeles.mod_id = machines.mac_mod_id'.
		" WHERE mac_cli_id ='".$this->attribut['cli_id']."'";
	if ($result = $this->db->Query($sql)){
		$th = array('modèle', 'n° de série', 'n° de commission', 'Mise en service');
		$data= array();
		//$largeurs = array(190*0.25,190*0.25,190*0.2,190*0.2,190*0.1);
		while ($row = $result->fetch_assoc()) {
			$data[] = array($row['mod_code'],$row['mac_serie'],$row['mac_commission'],$row['mac_annee']);
		}
		$pdf->Tableau($th, $data, -1, $hauteur);
	}

	// Commentaires
	$pdf->Separateur($hauteur,'Commentaires',$hauteur);
	$sql="SELECT com_texte, com_date, uti_nom, uti_prenom".
		' FROM commentaires'.
		' JOIN utilisateurs ON commentaires.com_uti_id=utilisateurs.uti_id'.
		" WHERE com_cli_id='".$this->attribut['cli_id']."'".
		' ORDER BY com_date DESC';
	if ($nb_comm>1)
		$sql .= ' LIMIT 0, '.$nb_comm;
	if ($result = $this->db->Query($sql)){
		while ($row = $result->fetch_assoc()) {
			$date = date("\l\e d/m/Y à H:i:s", strtotime($row['com_date']));
			$pdf->SetStyle(PDF_EM);
			$pdf->Cell(0, $hauteur, utf8_decode($date.' par '.$row['uti_nom'].', '.$row['uti_prenom']),0,1);
			$pdf->SetStyle(PDF_INFO);
			$pdf->MultiCell(0, $hauteur, utf8_decode($row['com_texte']), 0);		
		}
	}
	$pdf->AddY($hauteur);
						
	// Sortie
	$pdf->Output($this->attribut['cli_nom'].'.pdf','D');				
}
}


$c = new clientpdf($db);
$id = get_param('id', 268);
$c->Lire($id);
$c->Pdf();


echo 'fin';
?>
